package org.example.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.admin.exception.BizException;
import org.example.admin.mapper.UserMapper;
import org.example.admin.mapper.UserSystemMapper;
import org.example.admin.service.UserSystemService;
import org.example.admin.utils.DateUtils;
import org.example.admin.utils.ExportExcel;
import org.example.admin.utils.ImportExcel;
import org.example.entity.User;
import org.example.entity.UserSystem;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("userSystemServiceImpl")
public class UserSystemServiceImpl extends ServiceImpl<UserSystemMapper, UserSystem> implements UserSystemService {

    @Value("${excelTempDir}")
    private String excelTempDir;

    @Resource
    private UserMapper userMapper;

    @Override
    public String generateExcel(String fileName,List<User> allUsers) throws BizException {
        String fullPath = null;

        try {
//            List<User> allUsers = userMapper.selectList(Wrappers.emptyWrapper());

            String title = "用户信息";
            String[] rowNames = {"ID", "姓名", "邮件", "性别", "电话", "状态", "余额", "注册时间"};

            List<Object[]> userObjs = new ArrayList<>();

            allUsers.forEach(u -> {
                Object[] obj = {
                        u.getUId(),
                        u.getUName(),
                        u.getUEmail(),
                        "M".equals(u.getUSex()) ? "男" : "女",
                        u.getUPhone(),
                        1 == u.getUStatus() ? "激活" : "禁用",
                        u.getUBalance(),
                        DateUtils.transferDateToStr(u.getURegisterDate(), DateUtils.DATE_24FULL_STR)
                };
                userObjs.add(obj);
            });

            ExportExcel exportExcel = new ExportExcel(title, rowNames, userObjs);

            //根据时间生成文件名  20200630164834.xls
//            String fileName = DateUtils.transferDateToStr(new Date(), DateUtils.DATE_KEY_STR) + ".xls";
            fullPath = excelTempDir + fileName;

            exportExcel.export(fullPath);
        }catch (Exception ex) {
            ex.printStackTrace();
            throw new BizException("业务层生成Excel异常: " + ex.getMessage());
        }
        return fullPath;
    }



    @Override
    public void parseExcelAndInsertData(InputStream inputStream) throws BizException {

        ImportExcel importExcel = new ImportExcel();

        List<Object[]> list = importExcel.readExcelContent(inputStream);  //解析数据

        List<User> userList = new ArrayList<>();

        for(Object[] objects : list) {
            User user = new User();

            if (objects[0]!=null&&objects[1]!=null&&!"".equals(objects[1])){
                user.setUName(objects[1].toString());
                user.setUEmail(objects[2].toString());
                user.setUSex("男".equals(objects[3].toString()) ? "M" : "F");
                user.setUPhone(objects[4].toString());
                user.setUStatus("激活".equals(objects[5].toString()) ? 1 : 0);
                user.setURegisterDate(DateUtils.strToDate(objects[6].toString(), DateUtils.DATE_24FULL_STR));

                userList.add(user);
            }

        }

        if(userList.size() > 0) {
            int num = userMapper.batchInsertUsers(userList);
            System.out.println(num);
        }
    }

}
